var $ = {
    ajax: function (option = { type: "get", data: "" }) {
        var req = new XMLHttpRequest();

        if (option.type == "post") {
            req.open(option.type, option.url, true);
            if (typeof option.data == "object") {
                var fd = new FormData();
                for (var key in option.data) {
                    fd.set(key, option.data[key]);
                }
                req.send(fd);
            } else {
                req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                req.send(option.data);
            }
        } else {
            if (typeof option.data == "object") {   //如果是对象  遍历对象 拼接成 字符串
                var str = "";
                for (var key in option.data) {
                    // console.log(key, option.data[key]);//  key => "user"/"pwd"
                    str += key + "=" + option.data[key] + "&";
                }
                str = str.substr(0, str.length - 1);

            } else {
                str = option.data;
            }

            req.open(option.type, option.url + "?" + str, true);
            req.send();
        }
        req.onreadystatechange = function () {
            if (req.readyState == 4 && req.status == 200) {
                var result = req.responseText;
                // console.log(result);
                // do...something
                if (option.dataType == "json") {
                    result = JSON.parse(result);
                }
                if (option.callback) {
                    option.callback(result);
                }
            }
        }
    },
    get: function (option = { type: "get", data: "" }) {
        var req = new XMLHttpRequest();
        if (typeof option.data == "object") {   //如果是对象  遍历对象 拼接成 字符串
            /* {
                user: "wwww",
                pwd: 123132
               }   
            =>  user=wwww&pwd=123132  */
            var str = "";
            for (var key in option.data) {
                // console.log(key, option.data[key]);//  key => "user"/"pwd"
                str += key + "=" + option.data[key] + "&";
            }
            str = str.substr(0, str.length - 1);

        } else {
            str = option.data;
        }
        // console.log(str);
        req.open(option.type, option.url + "?" + str, true);
        req.send();
        req.onreadystatechange = function () {
            if (req.readyState == 4 && req.status == 200) {
                var result = req.responseText;
                if (option.dataType == "json") {
                    result = JSON.parse(result);
                }
                if (option.callback) {
                    option.callback(result);
                }
            }
        }
    },
    post: function (option) {
        var req = new XMLHttpRequest();
        req.open(option.type, option.url, true);
        if (typeof option.data == "object") {
            var fd = new FormData();
            // fd.set("user","asdasd");
            for (var key in option.data) {
                // console.log(key, option.data[key]); // key  "string"  =>  "user"/"pwd"
                fd.set(key, option.data[key]);
            }
            req.send(fd);
        } else {
            req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            req.send(option.data);
        }
        req.onreadystatechange = function () {
            if (req.readyState == 4 && req.status == 200) {
                var result = req.responseText;
                // console.log(result);
                // do...something
                if (option.dataType == "json") {
                    result = JSON.parse(result);
                }
                if (option.callback) {
                    option.callback(result);
                }
            }
        }
    }
}
export{$};